package com.gzwh.v1.sysapi.conntroller.dinner;

import com.gzwh.v1.sysapi.entity.dinner.AskDetailed;
import com.gzwh.v1.sysapi.entity.dinner.AskType;
import com.gzwh.v1.sysapi.entity.member.SFMemberEntity;
import com.gzwh.v1.sysapi.server.dinner.AskDetailedService;
import com.gzwh.v1.sysapi.server.dinner.AskTypeService;
import com.gzwh.v1.sysapi.server.dinner.DinnerVerificationService;
import com.gzwh.v1.sysapi.utils.ReturnUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订餐系统要求管理功能
 * <p>
 * 时间：2019-09
 * s
 */
@CrossOrigin(maxAge = 36000)
@RestController
@RequestMapping("/api/dinner/ask")
public class DinnerAskController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AskTypeService askTypeService;

    @Autowired
    AskDetailedService askDetailedService;


    @Autowired
    DinnerVerificationService dinnerVerificationService;

    /**
     * 分类总表创建
     * 时间：2019-09
     *
     * @return
     */
    @RequestMapping("/creat/type")
    @RequiresAuthentication
    public ModelMap saveType(@RequestParam("name") String name, @RequestParam("pid") Long pid, @RequestParam("appctionId") Long appid) {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        logger.info("当前用户【 " + user.getName() + " 】正在创建订餐系统要求分类，当前创建名称【   " + name + "   】,创建的分类id为【 " + pid + " 】");
        //创建查询appcation是否符合当前应用，不符合不可以创建
        if (!dinnerVerificationService.veriAppctionByAppid(appid, user.getMemberId())) {
            return ReturnUtil.Error("当前appctionId已失效，不可创建分类");
        }
        AskType askType = new AskType();
        askType.setName(name);//设置分类的名称
        askType.setParentId(pid);//设置父类id
        askType.setOperatorId(user.getMemberId());//设置操作员id
        askType.setAppId(appid);
        askTypeService.insertAskType(askType);
        return ReturnUtil.Success("操作完成", askType);
    }

    /**
     * 获取该应用下的所有分类信息
     * <p>
     * 时间：2019-09
     *
     * @param appid
     * @return
     */
    @RequestMapping("/get/type")
    @RequiresAuthentication
    public ModelMap getType(@RequestParam("appctionId") Long appid) {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        logger.info("当前用户【 " + user.getName() + " 】正在获取订单系统要求分类，获取的分类id【   " + appid + "   】");
        List<AskType> list = askTypeService.getAsktype(appid);
        return ReturnUtil.Success("操作完成", list);
    }

    /**
     * 删除该分类的信息，为了方便信息能寻找回来，则是在数据库中将该信息标志为1
     * <p>
     * 时间：2019-09
     *
     * @param appid
     * @return
     */
    @RequestMapping("/delete/type")
    @RequiresAuthentication
    public ModelMap deleteType(@RequestParam("appctionId") Long appid) {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        logger.info("当前用户【 " + user.getName() + " 】正在删除订单系统要求分类，获取的分类id【   " + appid + "   】");
        askTypeService.deleteAskType(appid);
        return ReturnUtil.Success("操作完成");
    }

    //******************************分界线,下面为分类详细内容*********************

    /**
     * 创建详细分类下的信息
     * <p>
     * 时间2019-09
     *
     * @param name  名称
     * @param pid  分类id
     * @return
     */
    @RequestMapping("/creat/detailed")
    @RequiresAuthentication
    public ModelMap saveDetailed(@RequestParam("name") String name, @RequestParam("pid") Long pid, @RequestParam("price") BigDecimal price) {
        SFMemberEntity user = (SFMemberEntity) SecurityUtils.getSubject().getPrincipal();//获取用户
        logger.info("当前用户【 " + user.getName() + " 】正在创建订餐系统要求分类详细，当前创建名称【   " + name + "   】,创建的父id为【 " + pid + " 】");
        try {
            AskDetailed askDetailed = new AskDetailed();
            askDetailed.setAskTypeId(pid);
            askDetailed.setName(name);
            askDetailed.setOperatorId(user.getMemberId());
            askDetailed.setPrice(price);
            askDetailedService.insertAskDateiled(askDetailed);
            return ReturnUtil.Success("操作完成", askDetailed);
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.Error("操作失败");
        }
    }

    /**
     * 删除详细分类
     * <p>
     * 时间：2019-09
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete/detailed")
    @RequiresAuthentication
    public ModelMap deleteDetailed(@RequestParam("id") Long id) {
        askDetailedService.deleteAskDetailes(id);
        return ReturnUtil.Success("操作完成");
    }

    

}
